<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="../../../resources/styles.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="icon" type="image/x-icon" href="../../../resources/favicon.ico">
  <title>Tools Component</title>
  <style>
      .ifcjs-dimension-label {
          background-color: black;
          font-family: sans-serif;
          color: white;
          padding: 8px;
          border-radius: 8px;
          pointer-events: all;
          transition: background-color 200ms ease-in-out;
      }

      .ifcjs-dimension-label:hover {
          background-color: grey;
      }

      .ifcjs-dimension-preview {
          pointer-events: none;
          background-color: #ffffff;
          width: 2rem;
          height: 2rem;
          opacity: 0.3;
          padding: 8px;
          border-radius: 100%;
      }
      body {
        margin: 0;
        padding: 0;
      }
      .full-screen {
        width: 100vw;
        height: 100vh;
        position: relative;
        overflow: hidden;
      }
  </style>
</head>
<body>
<div class="full-screen" id="container"></div>
<script type="importmap">
    {
      "imports": {
        "three": "https://unpkg.com/three@0.160.1/build/three.module.js",
        "stats.js/src/Stats.js": "https://unpkg.com/stats-js@1.0.1/src/Stats.js",
        "three/examples/jsm/libs/lil-gui.module.min": "https://unpkg.com/three@0.160.1/examples/jsm/libs/lil-gui.module.min.js",
        "openbim-components": "../../../resources/openbim-components.js"
      }
    }
</script>
<script type="module">

	// Set up scene (see SimpleScene tutorial)

	import * as THREE from 'three';
	import * as OBC from 'openbim-components';
	import Stats from 'stats.js/src/Stats.js';

	const container = document.getElementById('container');

	const components = new OBC.Components();

	components.scene = new OBC.SimpleScene(components);
	components.renderer = new OBC.PostproductionRenderer(components, container);
	components.camera = new OBC.SimpleCamera(components);
	components.raycaster = new OBC.SimpleRaycaster(components);

	components.init();

	components.renderer.postproduction.enabled = true;

	const scene = components.scene.get();

	components.camera.controls.setLookAt(10, 10, 10, 0, 0, 0);
	components.scene.setup();

  const grid = new OBC.SimpleGrid(components, new THREE.Color(0x666666));
	const effects = components.renderer.postproduction.customEffects;
	effects.excludedMeshes.push(grid.get());

	const dimensions = new OBC.VolumeMeasurement(components);

	let fragments = new OBC.FragmentManager(components);
	const file = await fetch("../../../resources/small.frag");
	const data = await file.arrayBuffer();
	const buffer = new Uint8Array(data);
	fragments.load(buffer);

	const highlighter = new OBC.FragmentHighlighter(components, fragments);
	highlighter.setup();
	highlighter.updateHighlight();

	highlighter.events.select.onHighlight.add((event) => {
		const fragmentIDs = Object.keys(event);
		const meshes = [];
		for(const id of fragmentIDs) {
			const frags = fragments.list[id].fragments;
			if(frags.select) {
				meshes.push(frags.select.mesh);
      }
    }
		const volume = dimensions.getVolumeFromMeshes(meshes);
		console.log(volume);
  })

  highlighter.events.select.onClear.add(() => {
		dimensions.label.get().removeFromParent();
  })

	container.ondblclick = () => dimensions.create();
	container.oncontextmenu = () => dimensions.endCreation();

	window.onkeydown = (event) => {
		if (event.code === 'Delete' || event.code === 'Backspace') {
			// WORK IN PROGRESS
			// dimensions.delete();
		}
	}

	const mainToolbar = new OBC.Toolbar(components, { name: 'Main Toolbar', position: 'bottom' });
	mainToolbar.addChild(dimensions.uiElement.get("main"));
	components.ui.addToolbar(mainToolbar);

	// Set up stats

	const stats = new Stats();
	stats.showPanel(2);
	document.body.append(stats.dom);
	stats.dom.style.left = '0px';
	const renderer = components.renderer;
	renderer.onBeforeUpdate.add(() => stats.begin());
	renderer.onAfterUpdate.add(() => stats.end());

</script>
</body>
</html>
